// ChatterServer
// Receives XML-RPC messages sent by Client on the client side
// and sends XML-RPC messages to ChatterClient on the client side

package edu.uci.ics.xmlrpc.webserver;

import java.net.URL;
import java.util.HashMap;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
import org.apache.xmlrpc.webserver.XmlRpcServlet;

public class ChatterServer extends XmlRpcServlet
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final int clientPort = 8080;
	
	// ordinarily there would be a database here instead, but fuck it for testing purposes
	HashMap<String,ChatUser> userMap = new HashMap<String,ChatUser>();
	
	public ChatterServer()
	{
		userMap.put("Dan", new ChatUser("Dan", "169.234.3.86"));
		userMap.put("Chad", new ChatUser("Chad", "169.234.3.249"));
		userMap.put("Matt", new ChatUser("Matt", "169.234.14.7"));
		userMap.put("Alex", new ChatUser("Alex", "169.234.12.54"));
		userMap.put("Josh", new ChatUser("Josh", "169.234.15.214"));
		userMap.put("Scott", new ChatUser("Scott", "169.234.10.154"));
		userMap.put("Client6", new ChatUser("Client6", "127.0.0.1"));
	}
	
	// echo a given message
	public String echoMessage(String message)
	{
		return message;
	}
	
	// MESSAGE RE-ROUTING TO DIFFERENT USERS IS DONE HERE
	public String sendMessageTo(String userFrom, String userTo, String message) throws Exception
	{
		// send an XML-RPC message to the client's XML-RPC server (ChatterClient)
		
		// get the user out of the HashMap
		ChatUser userToConfig = userMap.get(userTo);
		
		// figure out the client address based upon the client's configuration
    	String server = "http://" + userToConfig.getIPAddress() + ":" + clientPort + "/xmlrpc";
    	
    	// create configuration
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL(server));
        config.setEnabledForExtensions(true);  
        config.setConnectionTimeout(60 * 1000);
        config.setReplyTimeout(60 * 1000);

        XmlRpcClient client = new XmlRpcClient();
      
        // use Commons HttpClient as transport
        client.setTransportFactory(
            new XmlRpcCommonsTransportFactory(client));
        // set configuration
        client.setConfig(config);

        System.out.println("Received message from " + userFrom + ": " + message);
        // make the a regular call
        Object[] params = new Object[]
            { new String(userFrom), new String(message) };
        client.execute("ChatterClient.receiveMessageFrom", params);
        System.out.println("Re-routed to " + userTo + ": " + message);
        
        return "true";
	}
}

class ChatUser
{
	private String username;
	private String ipAddress;
	
	public ChatUser(String username, String ipAddress)
	{
		this.username = username;
		this.ipAddress = ipAddress;
	}
	
	public String getUsername() {return username;}
	public String getIPAddress() {return ipAddress;}
}
